﻿Chenxin.UI.VacationApply = Chenxin.extend(Chenxin.UI.PageCommon,{
    autoInitForm:false,
    actionUrl:'/service/vacationApply.ashx',
    createGrid:function(){
		return new Chenxin.UI.Pagination({
			actionUrl:this.actionUrl + "?op=Search",
			pageSize:15,
			renderTo: 'house_table'
		});
	},
	createForm:function(){
        return new Chenxin.UI.EditVacForm({
            scope:this,
            afterGetFormJson:function(json){
                json.UserName = this.form.comboList[1].getText();
                json.DepartName = this.form.comboList[0].getText();
            }
        });
    },
	createSearch:function(){
		return new Chenxin.UI.VacationApplySearch({
			scope:this
		});
	},
	initEvent:function(){
	    objVacApply = this;

	    /*************************事件：月份得到焦点************************/
        $("#YearMonth").focus(function(){
            WdatePicker({dateFmt:'yyyy-MM',oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        });
    
	    /*************************事件：列表行点击事件************************/
	    $('.tr_houselist').live('click',function(){	
			$('.tr_houselist,.selected_orgbgcolor').each(function(){
				$(this).removeClass('selected_orgbgcolor');	
			});
			
			$(this).toggleClass('selected_orgbgcolor');	
			if(!$('#tr-house-info').length){
				objVacApply.currentId=this.id;
				
			}else{
				$('#tr-house-info').remove();
				if(objVacApply.currentId!=this.id){
					objVacApply.currentId=this.id;
				}
			}
		});
		/*************************事件：列表行鼠标滑过************************/
	    $('.tr_houselist').live('mouseover',function(){
			$(this).toggleClass('orgbgcolor');
		});
		/*************************事件：列表行鼠标离开************************/
		$('.tr_houselist').live('mouseout',function(){
			$(this).toggleClass('orgbgcolor');
		});
		
		/*************************事件：休假申请************************/
		$("#aApplyVac").click(function(){
            
            //删除按钮非显示
            $("#btDelEditVac").hide();
            objVacApply.currentId = "";
		    objVacApply.form.show();
		    objVacApply.form.reset();
			objVacApply.form.setTitle('休假申请');
			$('.tr_houselist,.selected_orgbgcolor').each(function(){
				$(this).removeClass('selected_orgbgcolor');	
			});
		});
		
		/*************************事件：假期编辑************************/
		$("#aEditVac").click(function(){
            //选中行编辑
		    if( objVacApply.currentId == "" ){
		        alert("请选择相应行进行编辑");
		        return;
		    }
		    
		    //删除按钮显示
            $("#btDelEditVac").show();
            
		    var data ={};
		  
		    data.RecordId = objVacApply.currentId;
		    data.UserId = $("#" + objVacApply.currentId).children("td:nth-child(2)").text();
		    data.VacationType = $("#" + objVacApply.currentId).children("td:nth-child(9)").text();
		    data.StartTime = $("#" + objVacApply.currentId).children("td:nth-child(6)").text();
		    data.EndTime = $("#" + objVacApply.currentId).children("td:nth-child(7)").text();
		    data.Days = $("#" + objVacApply.currentId).children("td:nth-child(8)").text();
		    data.Reason = $("#" + objVacApply.currentId).children("td:nth-child(10)").text();
		    data.DepartId = $("#" + objVacApply.currentId).children("td:nth-child(11)").text();
		    
		    objVacApply.form.reset();
		    objVacApply.form.currentModel=data;
		    objVacApply.form.fitData(data);
		    objVacApply.form.setTitle('假期编辑');
		    objVacApply.form.show();   

		});
		
	},
	initComponent:function(){
		Chenxin.UI.VacationApply.superclass.initComponent.call(this);
	}
});
/**
*假期信息检索
*/
Chenxin.UI.VacationApplySearch = Chenxin.extend(Chenxin.UI.SearchCommon,{
    id:'house_search',
	initEvent:function(){
		var vacApplySearch = this;
		/*************************事件：检索************************/
		$('#btSreachVac').click(function(){
			vacApplySearch.scope.reloadGrid();
		});
	},
	createComboList:function(){
		var cmbStruct = new Chenxin.UI.StructComboList();
		var cmbList = cmbStruct.comboList;
		return cmbList;
	},
	initComponent:function(){
		Chenxin.UI.VacationApplySearch.superclass.initComponent.call(this);
	}
});

/**
*假期编辑层
*/
Chenxin.UI.EditVacForm = Chenxin.extend(Chenxin.UI.FormCommon,{
    actionUrl:'/service/vacationApply.ashx',
	id:'jqmEditVac',
	renderTo:'divEditVac',
	winCls:'',
	jqmWidth:670,
	beforeFitData:function(model){
	    this.form.currentUserId = model.UserId;
	},
	createComboList:function(){
        var objFrm = this;
	    //申请部门
	    var selDepart = new Chenxin.UI.Combo({
	        containerId:"#DepartId",
	        tbxWidth:65,
	        required:true,
	        actionUrl:"/service/structhandler.ashx",
	        autoLoad:true,
	        colNum: 4,
            hasPage: true,
            pageSize: 40,
	        param:{op:"getcontroldeparts"},
	        afterSetValue:function(newVal){
				if(newVal==this.defVal){
					selApplier.clearItems();
				}else{
					selApplier.param.fatherid=newVal;
					selApplier.reload(objFrm.currentUserId);
				}
			},
	        onSelected:function(){
	            if(this.getValue()==this.defVal){
					selApplier.clearItems();
				}else{
	                selApplier.param.fatherid=this.getValue();
				    selApplier.reload();
				}
            }
	    });
	    //申请者
	    var selApplier = new Chenxin.UI.Combo({
            containerId:'#UserId',
            tbxWidth:65,
            colWidth:120,
            required:true,
            autoLoad:false,
            param:{op:"getemployee"},
            actionUrl:"/service/structhandler.ashx"
	    });
	    //类型
	    var selType = new Chenxin.UI.ParameterCombo({
            containerId:'#VacationType',
            tbxWidth:65,
            key:"System.Vacation",
            required:true,
            inModelType:"txt"
	    }); 
		return [selDepart,selApplier,selType];
	},
	initFormValidator:function(){
        var objVacForm = this;
        //开始日期
        $("#StartTime").focus(function(){
            WdatePicker({oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        }).formValidator({validatorgroup:this.id,
            onshow:"请输入开始日期！",
            onfocus:"请输入开始日期",
            oncorrect:"OK"
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "开始日期不能为空!" }, 
            type:"date",
            onerror:"开始日期不能为空!"
        });
        
        //结束日期
        $("#EndTime").focus(function(){
            WdatePicker({oncleared:function(){$(this).blur();},onpicked:function(){$(this).blur();}})
        }).formValidator({validatorgroup:this.id,
            onshow:"请输入结束日期！",
            onfocus:"请输入结束日期",
            oncorrect:"OK"
        }).inputValidator({
            empty: { leftempty: false, rightempty: false, emptyerror: "结束日期不能为空!" }, 
            type:"date",
            onerror:"结束日期不能为空!"
        }).functionValidator({
            fun:function(){
                if($("#EndTime").val()<$("#StartTime").val()){
                    return false;
                }else{
                    return true;
                }
            },
            onerror:"结束日期不能比开始日期早"
        });

        //请假天数
        $("#Days").formValidator({validatorgroup:this.id,
            onshow:"最小单位为0.5天！",
            onfocus:"最小单位为0.5天",
            oncorrect:"OK"
        }).inputValidator({
            min: 1, 
            empty: { leftempty: false, rightempty: false, emptyerror: "请假天数不能为空!" }, 
            onerror: "请假天数不能为空!"
        }).regexValidator({
            regexp:"decmal6",
            datatype:"enum",
            onerror:"格式不正确"
        }).functionValidator({
            fun:function(){
                if($("#StartTime").val()!="" & $("#EndTime").val()!=""){
                    var dtStart = new Date($("#StartTime").val().replace(/-/g,"/"));
                    var dtEnd = new Date($("#EndTime").val().replace(/-/g,"/"));
                    var intGapDays = (dtEnd-dtStart)/(24*60*60*1000) + 1;
                    var intDays = parseFloat($("#Days").val());
                    if( intDays>intGapDays ){
                        return false;
                    }else{
                        return true;
                    }
                }
            },
            onerror:"天数不正确"
        }).functionValidator({
            fun:function(){
                var strVacType = objVacForm.comboList[2].getText();
                var strRecordId = objVacForm.scope.currentId;
                var strUserId = objVacForm.comboList[1].getValue();
                
                var bRet = true;
                //年假的场合
                if( strVacType == "年假" ){
                    
                    if( strUserId == objVacForm.comboList[1].defVal ){
                        return true;
                    }    
                    
                    Chenxin.ajax({
				        type: "post",
				        async:false,
				        dataType: "json",
				        url: objVacForm.actionUrl,
				        data: {op:'ChkAnnualVac',UserId:strUserId,RecordId:strRecordId},
				        onSuccess: function(data) {
					        //年假不足
                            if( parseFloat($("#Days").val()) > data ){
                                bRet=false;
                            }
				        }
			        });
                }
                return bRet;
            },
            onerror:"年休假数已不够"
        });
        
        //理由    
        $("#Reason").formValidator({validatorgroup:this.id,
            onshow:"请输入请假理由！",
            onfocus:"请输入请假理由",
            oncorrect:"OK"
        }).inputValidator({
            min: 1, 
            empty: { leftempty: false, emptyerror: "请假理由不能为空!" }, 
            onerror: "请假理由不能为空!"
        });
	},
	createForm:function(){
		return formCommon.editVacForm('EditVac');
	},
	initEvent:function(){
	    objEditForm = this;
	    /*************************事件：提交************************/
	    $("#btSubmitEditVac").live("click",function(){

	        if(objEditForm.scope.currentId == ""){
	            objEditForm.op = "Add";
	        }else{
	            objEditForm.op = "Edit";
	        }
	        objEditForm.save();
	    });
	    /*************************事件：删除************************/
	    $("#btDelEditVac").live("click",function(){

	        objEditForm.del(objEditForm.currentModel.RecordId,"Del");
	    });
	    /*************************事件：关闭************************/
	    $("#btCloseEditVac").live("click",function(){
	        objEditForm.hide();
	    });
	},
	saveSucc:function(){
	    //刷新
	    this.reloadGrid();
	},
	afterDelSucc:function(){
	    this.reloadGrid();
	},
	initComponent:function(){
		Chenxin.UI.EditVacForm.superclass.initComponent.call(this);
	}
});

$(document).ready(function(){

    var vacApply = new Chenxin.UI.VacationApply();
});
